package com.JUC.cha03.sec03;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class FutureAPIDemo {

    public static void main(String[] args) throws ExecutionException, InterruptedException, TimeoutException {
        FutureTask futureTask = new FutureTask<>(() -> {
            System.out.println("---- come in task");
            TimeUnit.SECONDS.sleep(5);
            return "task over";
        });

        Thread t1 = new Thread(futureTask, "t1");
        t1.start();

        System.out.println(Thread.currentThread().getName() + "\t ----忙其他任务");

//        System.out.println(futureTask.get());// 必须等到结果才会离开，不管你是否计算完成，容易程序堵塞
//        futureTask.get(3, TimeUnit.SECONDS);

        while (true){
            if (futureTask.isDone()){
                System.out.println(futureTask.get());
                break;
            }else {
                System.out.println("正在执行");
                Thread.sleep(500);
            }
        }


    }
}
